<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 小夏 < 449134904@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\ActivityModel;
use app\admin\model\CommonModel;
use cmf\controller\AdminBaseController;
use think\Db;

//活动管理
class ActivityController extends AdminBaseController
{
    public function index(){
        $param = $this->request->param();
        $is_rotation = ActivityModel::ROTATION;
        $is_hidden = ActivityModel::IS_HIDDEN;
        $status = ActivityModel::STATUS;
        $list = Db::name('activity')
            ->where(function($query) use($param){
                if(isset($param['title']) && !empty($param['title'])){
                    $title = $param['title'];
                    $query->where('title','like','%'.$title.'%');
                }

                if (isset($param['time']) && !empty($param['time'])) {
                    $time = strtotime($param['time']);
                    $where[] = ['start_time','<=',$time];
                    $where[] = ['end_time','>=',$time];
                    $query->where($where);
                }
            })
            ->field('content,update_time',true)
            ->order('id desc')
            ->paginate(null,false,['query'=>$param])
            ->each(function($item, $key) use($is_rotation,$is_hidden,$status){
                $item['is_rotation'] = $is_rotation[$item['is_rotation']];
                $item['is_hidden'] = $is_hidden[$item['is_hidden']];
                $item['status'] = $status[$item['status']];
                return $item;
            });
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
        return $this->fetch();
    }

    //添加页面
    public function add(){
        //是否轮播
        $this->assign('is_rotation',ActivityModel::ROTATION);
        //是否隐藏
        $this->assign('is_hidden',ActivityModel::IS_HIDDEN);
        return $this->fetch();
    }

    //添加提交
    public function addPost(){
        $data = $this->request->param();
        $result = $this->validate($data,'Activity.add');
        if($result !== true){
            $this->error($result);
        }

        $commonModel = new CommonModel();
        $data['content'] = $commonModel->setPostContentAttr($data['content']);
        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);
        //过滤data数组中的非数据表字段数据
        $activityModel = new ActivityModel();
        $res = $activityModel->allowField(true)->create($data);
        if($res){
            $this->success('添加成功!', url('Activity/index'));
        }else{
            $this->error('添加失败');
        }
    }

    //编辑页面
    public function edit(){
        $id = $this->request->param('id');
        $post = Db::name('activity')
            ->where(['id'=>$id])
            ->find();
        $commonModel = new CommonModel();
        $post['content'] = $commonModel->getPostContentAttr($post['content']);
        $this->assign('post',$post);

        //是否轮播
        $this->assign('is_rotation',ActivityModel::ROTATION);
        //是否隐藏
        $this->assign('is_hidden',ActivityModel::IS_HIDDEN);
        return $this->fetch();
    }

    //编辑提交
    public function editPost(){
        $data = $this->request->param();
        $result = $this->validate($data,'Activity.edit');
        if($result !== true){
            $this->error($result);
        }

        $commonModel = new CommonModel();
        $data['content'] = $commonModel->setPostContentAttr($data['content']);
        $data['start_time'] = strtotime($data['start_time']);
        $data['end_time'] = strtotime($data['end_time']);
        //过滤data数组中的非数据表字段数据
        $activityModel = new ActivityModel();
        $res = $activityModel->allowField(true)->save($data,['id'=>$data['id']]);
        if($res){
            $this->success('保存成功!', url('Activity/index'));
        }else{
            $this->error('保存失败');
        }
    }

    //删除
    public function delete(){
        $id = $this->request->param('id');
        $activityModel = new ActivityModel();
        $res = $activityModel->where('id',$id)->delete();
        if($res){
            $this->success('删除成功！');
        }else{
            $this->error('删除失败！');
        }
    }
}